package com.mvp.shq.washer.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.entity.shop.Product;
import com.mvp.shq.washer.rxbus.BusData;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.utils.DataDictionary;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.ImageUtil;
import com.mvp.shq.washer.utils.StringUtils;
import com.orhanobut.logger.Logger;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by westing on 2017/10/10.
 * 购物车列表
 */

public class ShopcarsetAdapter extends RecyclerView.Adapter<ShopcarsetAdapter.ViewHolder> {



    private Context context;
    private List<Product> productlist;
    private ImageUtil imageUtil;
    private ClickSetNumLiserent clickSetNumLiserent;

    public List<Product> getProductlist() {
        return productlist;
    }

    public void setProductlist(List<Product> productlist) {
        this.productlist = productlist;
    }

    public ShopcarsetAdapter(Context mContext, List<Product> productlist, ClickSetNumLiserent clickSetNumLiserent) {
        this.context = mContext;
        this.productlist = productlist;
        this.clickSetNumLiserent=clickSetNumLiserent;
        imageUtil = new ImageUtil(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shopcarset, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        int test = 0;
        Product product = productlist.get(position);
        holder.tvByType.setText(product.getSpecText()+"");
        String imgUrl = DataDictionary.SHOP_IMG_BASE_URL+product.getImgUrl().replace(DataDictionary.SHOP_IMG_BASE_URL,"");
        imageUtil.loadUrlImage(imgUrl,holder.imgProductPic);
        holder.num.setText(""+product.getByNum());
//        if(checkAll){
//           holder.radiochange.setBackgroundResource(R.mipmap.cart_selected);//选择
//        }else{
//
//        }
        if(product.getIsCheck()){
            holder.radiochange.setBackgroundResource(R.mipmap.cart_selected);
        }else{
            holder.radiochange.setBackgroundResource(R.mipmap.cart_pressed);
        }
        holder.radiochange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickSetNumLiserent.onCheckItem(position);
            }
        });
        holder.tvByType.setVisibility(View.GONE);
        holder.tvBySize.setVisibility(View.GONE);
        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nums = holder.num.getText().toString();
                int n = Integer.parseInt(nums);
                Logger.d("数量之前：====n="+n);
                n++;
                notifyDataSetChanged();
                clickSetNumLiserent.changeNum(n,position);
            }
        });
        holder.subtract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nums = holder.num.getText().toString();
                int n = Integer.parseInt(nums);
                if (n != 0) {
                    n--;
                }
                notifyDataSetChanged();
                clickSetNumLiserent.changeNum(n,position);
            }
        });
        holder.bntDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickSetNumLiserent.delete(position);
            }
        });
        holder.imgProductPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickSetNumLiserent.itemOnClick(position);
            }
        });
    }

    private boolean checkAll;

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public boolean isCheckAll() {
        return checkAll;
    }

    public void setCheckAll(boolean checkAll) {
        this.checkAll = checkAll;
        for (int i=0,len=productlist.size();i<len;i++){
            productlist.get(i).setCheck(checkAll);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return productlist != null ? productlist.size() : 0;
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.radiochange)
        ImageView radiochange;
        @BindView(R.id.img_product_pic)
        ImageView imgProductPic;
        @BindView(R.id.subtract)
        Button subtract;//减
        @BindView(R.id.num)
        EditText num;//数量
        @BindView(R.id.add)
        Button add;//加
        @BindView(R.id.tv_by_type)
        TextView tvByType;//类型
        @BindView(R.id.tv_by_size)
        TextView tvBySize;//规格
        @BindView(R.id.bnt_delete)
        TextView bntDelete;//删除
        @BindView(R.id.item_product)
        LinearLayout itemProduct;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }
    public interface ClickSetNumLiserent{
        void changeNum(int num,int childrenPosition);
        void delete(int num);
        void itemOnClick(int position);
        //选中
        void onCheckItem(int position);
    }
}
